const _request = require("../../utils/request.js");
const app = getApp()
var timerse;
Page({
  data: {
    show1: false,
    orderid: '', //订单id
    datainfo: '', //订单详情
    shopimgUrl: app.globalData.shopimgUrl,
    show_time:'',//倒计时
  },
  onLoad: function (options) {
    this.setData({
      orderid: options.orderid
    })
    this.getinfo()
  },
  getinfo() {
    _request.get('/order/getorderinfo', {
      id: this.data.orderid
    }, app.globalData.mallType).then((res) => {
      if (res.data.code === 200) {
        var _info =  res.data.data
        _info.orderItemList.forEach(e => {
          e.spObj = JSON.parse(e.sp1);
          let list = []
          e.spObj.forEach(item => {
            list.push(item.key + ":" + item.value)
          })
          e.spType =  list.join(',')
        })
        clearInterval(timerse)
        this.gettimerse()//倒计时
        this.setData({
          datainfo:_info
        })
        console.log(this.data.datainfo)
      }else{
        wx.showToast({
          title: res.data.message,
          icon:'none'
        })
      }
    })
  },
  gettimerse(){
    timerse = setInterval(() => {
      let createordertime = this.data.datainfo.createtimelong / 1000 + 86400 //结束待支付时间
      createordertime--;
      let now = new Date().getTime() / 1000; //当前时间换时间戳
      let diff = parseInt(createordertime - now);
      if (diff < 0) {
        clearInterval(timerse)
        wx.navigateBack({ delta: 1 })
      } else {
        let date = this.formattime(diff * 1000);
        this.setData({
          show_time: date
        })
      }
    }, 1000)
  },
  //格式化时间
  formattime(time) {//格式化时间戳
    var leave1 = time % (24 * 3600 * 1000) //计算小时    
    var hour = Math.floor(leave1 / (3600 * 1000))
    var leave2 = leave1 % (3600 * 1000) //计算分钟    
    var minute = Math.floor(leave2 / (60 * 1000))
    var leave3 = leave2 % (60 * 1000) //计算秒    
    var second = Math.round(leave3 / 1000) == '60' ? '00' : Math.round(leave3 / 1000)
    return [hour, minute, second].map(this.formatNumber).join(':')
  },
  formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  },
  // 操作
  czorder(e) {
    var _item = this.data.datainfo
    var that = this
    if (e.currentTarget.dataset.type == 1) { //点击取消订单
      wx.showModal({
        title: '温馨提示',
        content: '确认取消订单?',
        cancelText: '取消',
        confirmText: "确认",
        success(res) {
          if (res.confirm) {
            _request.postObj('/order/updateordercancel', {
              id:_item.id,
              note:'',
            }, app.globalData.mallType).then((res) => {
              console.log(res)
              if (res.data.code === 200) {
                that.getinfo()
              }else{
                wx.showToast({
                  title: res.data.message,
                  icon:'none'
                })
              }
            })
          }
        }
      })
    } else if (e.currentTarget.dataset.type == 2) { //去付款
      wx.navigateTo({
        url: '/pages/shopingmall/payment?orderId=' + _item.id + '&orderSn=' + _item.orderSn + '&realityPayPrice=' + _item.payAmountStr + '&createordertime=' + _item.createtimelong,
      })
    } else if (e.currentTarget.dataset.type == 3) { //提醒发货
      _request.postObj('/order/remindDelivery', {
        orderId:_item.orderSn,
        userid:wx.getStorageSync('userid'),
      }, app.globalData.mallType).then((res) => {
        console.log(res)
        if (res.data.code === 200) {
          wx.showToast({
            title: '已提醒商家发货~',
            icon:'none'
          })
        }else{
          wx.showToast({
            title: res.data.message,
            icon:'none'
          })
        }
      })
    } else if (e.currentTarget.dataset.type == 4) { //确认收货
      wx.showModal({
        title: '温馨提示',
        content: '您确认收到了吗?',
        cancelText: '取消',
        confirmText: "确认",
        success(res) {
          if (res.confirm) {
            _request.postObj('/order/confirmreceipt', {
              id:_item.id,
            }, app.globalData.mallType).then((res) => {
              console.log(res)
              if (res.data.code === 200) {
                that.getinfo()
              }else{
                wx.showToast({
                  title: res.data.message,
                  icon:'none'
                })
              }
            })
          }
        }
      })
    } else if (e.currentTarget.dataset.type == 5) { //删除订单
      wx.showModal({
        title: '温馨提示',
        content: '请确认是否删除订单?',
        cancelText: '取消',
        confirmText: "确认",
        success(res) {
          if (res.confirm) {
            _request.postObj('/order/deleteorderinfo', {
              id:_item.id,
            }, app.globalData.mallType).then((res) => {
              console.log(res)
              if (res.data.code === 200) {
                that.getinfo()
              }else{
                wx.showToast({
                  title: res.data.message,
                  icon:'none'
                })
              }
            })
          }
        }
      })
    }
  },
  fuzhifun(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.num,
      success: function (res) {
        // wx.showModal({
        //   title: '提示',
        //   content: '复制成功',
        //   showCancel: false
        // });
      }
    })

  },
  openkefu() {
    this.setData({
      show1: !this.data.show1
    })
  },
  getshow1() {
    this.setData({
      show1: !this.data.show1
    })
  },
  callphone() {
    wx.makePhoneCall({
      phoneNumber: '044-820-8820'
    })
  },
})